package com.wufeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wufeng.entity.User;
import com.wufeng.service.UserService;
import com.wufeng.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Hxh
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2023-12-19 21:39:01
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Boolean updateAvatarByUserId(User user) {
        int i = userMapper.updateAvatarByUserId(user);
        return i > 0 ? true : false;
    }

    @Override
    public List<User> getUserListByConditions(User user, Integer pageIndex, Integer pageSize) {

        Integer startIndex = (pageIndex - 1) * pageSize;
        return userMapper.getUserListByConditions(user, startIndex, pageSize);
    }

    @Override
    public User getUserByUserName(String userName) {
        QueryWrapper<User> qw = new QueryWrapper<User>().eq("userName", userName);
        User user = userMapper.selectOne(qw);
        return user;
    }

    /**
     * 根据用户名模糊查询出id列表
     *
     * @param userName
     * @return
     */
    @Override
    public List<Integer> getUserIdsByUserName(String userName) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.like("userName", userName);
        List<User> userList = userMapper.selectList(qw);
        List<Integer> idList = userList.stream().map(user -> user.getUserid()).collect(Collectors.toList());
        return idList;
    }


    @Override
    public List<User> getUserListByUserName(String userName) {
        QueryWrapper<User> qw = new QueryWrapper<User>().like("userName", userName);
        return this.baseMapper.selectList(qw);
    }
}




